package com.gridmobi.xrtb.model.rtb;


import com.google.common.collect.Sets;
import com.gridmobi.xrtb.common.GeoMap;
import com.gridmobi.xrtb.domain.adn.OsE;
import com.gridmobi.xrtb.domain.dsp.common.enums.ExchangesE;
import com.gridmobi.xrtb.handler.ImprOffersHandler;
import com.gridmobi.xrtb.model.openrtb23.Imp;
import com.gridmobi.xrtb.model.openrtb23.OpenRTB23BidRequest;
import com.gridmobi.xrtb.util.RtbUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Set;

/**
 *
 */

public class AdmTemplate {
    private static final Logger logger = LoggerFactory.getLogger(AdmTemplate.class);


    public static String IMAGEAD_TEMPLATE = "<a href='__CLICKURL__' target='_blank'><img src='__IMAGEURL__' width='{creative_ad_width}px' height='{creative_ad_height}px'/></a><img src='__PIXELURL__' width='1px' height='1px' style='display:none'/>";
    public static String IMAGEAD_TEMPLATE_JS = "<a href='__CLICKURL__' target='_blank'><img src='__IMAGEURL__' width='{creative_ad_width}px' height='{creative_ad_height}px'/></a>" +
            "<div>\n" +
            "<!DOCTYPE html>\n" +
            "<html lang='en'>\n" +
            "<head>\n" +
            "    <meta charset='UTF-8'/>\n" +
            "    <meta name='viewport' content='width=device-width, initial-scale=1.0'/>\n" +
            "    <title>Document</title>\n" +
            "</head>\n" +
            "<body>\n" +
            "<img style=\"display: none\" height=\"1px\" width=\"1px\"\n" +
            "     src=\"__PIXELURL__\"/>\n" +
            "<script src='__CLIENTIMPR__'></script>\n" +
            "<script>\n" +
            "    markImpr('__OS__', '__GEO__', __NUM__,__SSPTYPE__)\n" +
            "</script>\n" +
            "</body>\n" +
            "</html>\n" +
            "</div>";

    public static String RICHMRAIDAD_TEMPLATE = "<!DOCTYPE html>\n" +
            "<html lang=\"en\">\n" +
            "<head>\n" +
            "    <meta charset=\"UTF-8\">\n" +
            "</head>\n" +
            "<body>\n" +
            "__CONTENT__" +
            "<img src='__PIXEL2URL__' width='1px' height='1px' style='display:none'/>" +
            "</body>\n" +
            "</html>";

    static String client_url = "https://dxmidwjvpoz1x.cloudfront.net/impr.js";
    static String client_url_eu = "https://dxmidwjvpoz1x.cloudfront.net/impreu.js";
    static Set<String> EU_GEOS = Sets.newHashSet("AUS", "UKR", "RUS", "FIN", "FIN", "PRT", "NLD", "DNK", "NOR", "SWE", "FRA", "CHL", "GBR", "ESP", "ITA", "USA", "CAN", "BRA", "DEU", "MEX");

    public static String createMsg(int script ,String channel, String cid, Imp imp, String clickUrl, String creativeUrl, String impUrl, String w, String h, String impUrl2, OpenRTB23BidRequest bidRequest) {
        String geo = RtbUtil.get23Country(bidRequest);
        String osV = RtbUtil.get23OS(bidRequest);
        int ssp = 0;
        if (ExchangesE.ALGORIX.key.equalsIgnoreCase(channel)) {
            ssp = ExchangesE.ALGORIX.code;
        }
        if (ExchangesE.SMAATO.key.equalsIgnoreCase(channel)) {
            ssp = ExchangesE.SMAATO.code;
        }
        if (ExchangesE.YEAHMOBI.key.equalsIgnoreCase(channel)) {
            ssp = ExchangesE.YEAHMOBI.code;
        }
        StringBuffer stringBuffer = new StringBuffer();
        String geo2 = GeoMap.word3Map.get(geo);
        String osKey = OsE.getKeyByV(osV);

        String sdkOfferKey = geo2 + osKey;
        boolean clientR = false;
        if(script==1){
            if (ImprOffersHandler.GEO_OS_MAP.containsKey(sdkOfferKey)) {
                clientR = true;
            }
        }
        if (ExchangesE.YEAHMOBI.key.equalsIgnoreCase(channel)) {

            String msg = new String(IMAGEAD_TEMPLATE).replace("__CLICKURL__", clickUrl)
                    .replace("__IMAGEURL__", RtbUtil.packCreativeUrl(imp.getSecure(), creativeUrl))
                    .replace("{creative_ad_width}", w)
                    .replace("{creative_ad_height}", h)
                    .replace("__PIXELURL__", impUrl)
                    .replace("__OS__", osV)
                    .replace("__GEO__", geo)
                    .replace("__NUM__", "50")
                    .replace("__CLIENTIMPR__", EU_GEOS.contains(geo) ? client_url_eu : client_url)
                    .replace("__SSPTYPE__", ssp + "");
            stringBuffer.append(msg);
        } else {

            String msg = new String(clientR ? IMAGEAD_TEMPLATE : IMAGEAD_TEMPLATE).replace("__CLICKURL__", clickUrl)
                    .replace("__IMAGEURL__", RtbUtil.packCreativeUrl(imp.getSecure(), creativeUrl))
                    .replace("{creative_ad_width}", w)
                    .replace("{creative_ad_height}", h)
                    .replace("__PIXELURL__", impUrl)
                    .replace("__OS__", osV)
                    .replace("__GEO__", geo)
                    .replace("__NUM__", "50")
                    .replace("__CLIENTIMPR__", EU_GEOS.contains(geo) ? client_url_eu : client_url)
                    .replace("__SSPTYPE__", ssp + "");
            stringBuffer.append(msg);
        }


        String resp = stringBuffer.toString();


        return resp;

    }

    public static String createmraidMsg(Campaign winCampaign, String script, String impUrl, String clickUrl, String impUrl2) {

        return new String(RICHMRAIDAD_TEMPLATE).replace("__CONTENT__", script.replaceAll("\\{CLICKURL}", clickUrl).replaceAll("\\{IMPURL}", impUrl)).replaceAll("__PIXEL2URL__", impUrl2);

    }

    public static String createSimpleMraidMsg(String imgurl, Integer w, Integer h, String impUrl, String clickUrl, String impUrl2) {

        return new String(RICHMRAIDAD_TEMPLATE).replace("__CONTENT__", createSimpleMraidBody(imgurl, w, h, impUrl, clickUrl, impUrl2));

    }

    /**
     * 非smaato 简单富媒体代码里要包含imp url 1像素的代码 包含了MRAID_BODY_IMP
     *
     * @param imgurl
     * @param w
     * @param h
     * @param impUrl
     * @param clickUrl
     * @return
     */
    public static String createSimpleMraidBody(String imgurl, Integer w, Integer h, String impUrl, String clickUrl, String impUrl2) {
        String body = new String(MRAID_BODY).replaceAll("__IMGURL__", imgurl).replaceAll("__IMGW__", w.toString()).replaceAll("__IMGH__", h.toString()).replaceAll("__CLICKURL__", clickUrl).replace("__IMPURL__", impUrl).replace("__PIXEL2URL__", impUrl2);
        return body;
    }

    /**
     * smaato 的富媒体里不需要1像素的imp url,因为他的xml adm里包含了 imp 节点,如果在这里在增加一个imp 代码会重复收到imp 请求
     *
     * @param imgurl
     * @param w
     * @param h
     * @param impUrl
     * @param clickUrl
     * @return
     */
    public static String createSmaatoSimpleMraidBody(String imgurl, Integer w, Integer h, String impUrl, String clickUrl, String impUrl2) {
        String body = new String(MRAID_BODY).replaceAll("__IMGURL__", imgurl).replaceAll("__IMGW__", w.toString()).replaceAll("__IMGH__", h.toString()).replaceAll("__CLICKURL__", clickUrl).replace("__IMPURL__", impUrl).replace("__PIXEL2URL__", impUrl2);
        return body;
    }

    /**
     * 简单的富媒体模板 包装图片 收集点击坐标
     */
    public static String MRAID_BODY = "" +
            "<img src=\"__IMGURL__\" width=\"__IMGW__\" height=\"__IMGH__\" border=\"0\" onclick=\"getMousePos(event)\">\n" +
            "<img src=\"__IMPURL__\" width=\"1px\" height=\"1px\" />\n" +
            "<img src=\"__PIXEL2URL__\" width=\"1px\" height=\"1px\" />\n" +
            "<script>\n" +
            "    function getMousePos(e) {\n" +
            "        var x = e.clientX;\n" +
            "        var y = e.clientY;\n" +
            "        var clickurl = \"__CLICKURL__&event=\"+x+\"x\"+y;\n" +
            "        if (window.top) {\n" +
            "            window.top.location.replace(clickurl)\n" +
            "        }else{\n" +
            "            window.location.replace(clickurl)\n" +
            "        }\n" +
            "    }\n" +
            "</script>";

    /**
     * 简单的imp 1像素 代码
     */
    public static String MRAID_BODY_IMP = "<img src=\"__IMPURL__\" width=\"1px\" height=\"1px\" />\n";

    public static void main(String[] args) {

        System.out.println("https://c.lazada.vn/t/c.ZCMQONidfa={idfa}".indexOf("?"));

    }

}

